##########################################################################################

library(dplyr)
library(data.table)
library(optparse)

##########################################################################################

option_list <- list(
    make_option(c("--msi_file"), type = "character") ,
    make_option(c("--depth_file"), type = "character") ,
    make_option(c("--mutNum_file"), type = "character") ,
    make_option(c("--info_file"), type = "character") ,
    make_option(c("--out_file"), type = "character")
)

if(1!=1){
    
    work_dir <- "~/20220915_gastric_multiple/dna_combine/"
    msi_file <- paste(work_dir,"/Qc/All_Msi.tsv",sep="")
    depth_file <- paste(work_dir,"/Qc/Depth_summary.txt",sep="")
    mutNum_file <- paste(work_dir,"/Qc/Vcf_QC.list",sep="")
	info_file <- paste(work_dir,"/config/tumor_normal.class.list",sep="")
    out_file <- paste(work_dir,"/Qc/Summary_Qc.tsv",sep="")

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

msi_file <- opt$msi_file
depth_file <- opt$depth_file
mutNum_file <- opt$mutNum_file
info_file <- opt$info_file
out_file <- opt$out_file

##########################################################################################

dat_msi <- data.frame(fread(msi_file))
dat_depth <- data.frame(fread(depth_file))
dat_mutNum <- data.frame(fread(mutNum_file))
dat_info <- data.frame(fread(info_file))

##########################################################################################
dat_msi <- dat_msi[,c("Tumor" , "Rate")]
colnames(dat_msi)[2] <- "msisensor"
dat_info <- merge( dat_info , dat_msi , by="Tumor" )

dat_depth <- dat_depth[,c("Tumor" , "TumorDepth" , "NormalDepth")]
dat_info <- merge( dat_info , dat_depth , by="Tumor" )

dat_mutNum <- dat_mutNum[,c("Tumor" , "SV_QC")]
colnames(dat_mutNum)[2] <- "mutNum"
dat_info <- merge( dat_info , dat_mutNum , by="Tumor" )

write.table( dat_info , out_file , row.names = F , sep = "\t" , quote = F )